package com.portal.logic;

import java.io.IOException;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.LinkedList;
import java.util.List;

public class CustomerDAO {

	//private String ip;
	private Connection connection;
	
	public CustomerDAO(Connection connection) {
		//this.ip = ip;
		this.connection = connection;
	}
	
	
	//Create
	public void insert() {
		Customer customer = null;
		try {
			Object deserializedObject = SerializationUtil.deserialize("c:\\customer.out");
			customer = (Customer) deserializedObject;
		} catch (ClassNotFoundException e1) {
			e1.printStackTrace();
		} catch (IOException e1) {
			e1.printStackTrace();
		}
		
		PreparedStatement preparedStatement = null;

		try {
			Statement st = connection.createStatement(); 
            st.executeUpdate("INSERT INTO tblCustomers " + 
                "VALUES ("+customer.getId()+", '"+customer.getName()+ "',"+customer.getAge()+",'"+customer.getResidentialArea()+"','"+customer.getPhone()+"',"+customer.getRating()+" )"); 

		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			if (preparedStatement != null) {
				try {
					preparedStatement.close();
				} catch (SQLException e) {
					e.printStackTrace();
				}
			}

			//ConnectionManager.closeConnection(connection);
		}
	}

	public Customer selectById(int id) {
		Customer customer = new Customer();
		PreparedStatement preparedStatement = null;
		ResultSet resultSet = null;

		try {
			preparedStatement = connection.prepareStatement("SELECT * FROM tblCustomers WHERE id = ?");
			preparedStatement.setInt(1, id);
			resultSet = preparedStatement.executeQuery();

			while (resultSet.next()) {
				customer.setId(resultSet.getInt("id"));
				customer.setName(resultSet.getString("name"));
				customer.setAge(Integer.parseInt(resultSet.getString("age")));
				customer.setResidentialArea(resultSet.getString("residentialArea"));
				customer.setPhone(resultSet.getString("phone"));
				customer.setRating(resultSet.getInt("rating"));
			}

		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			if (resultSet != null) {
				try {
					resultSet.close();
				} catch (SQLException e) {
					e.printStackTrace();
				}
			}
			if (preparedStatement != null) {
				try {
					preparedStatement.close();
				} catch (SQLException e) {
					e.printStackTrace();
				}
			}
			//ConnectionManager.closeConnection(connection);
		}

		return customer;
	}

	public Customer selectByName(String name) {
		Customer customer = new Customer();
		PreparedStatement preparedStatement = null;
		ResultSet resultSet = null;

		try {
			preparedStatement = connection.prepareStatement("SELECT * FROM tblCustomers WHERE name = ?");
			preparedStatement.setString(1, name);
			resultSet = preparedStatement.executeQuery();

			while (resultSet.next()) {
				customer.setId(resultSet.getInt("id"));
				customer.setName(resultSet.getString("name"));
				customer.setAge(Integer.parseInt(resultSet.getString("age")));
				customer.setResidentialArea(resultSet.getString("residentialArea"));
				customer.setPhone(resultSet.getString("phone"));
				customer.setRating(resultSet.getInt("rating"));
			}

		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			if (resultSet != null) {
				try {
					resultSet.close();
				} catch (SQLException e) {
					e.printStackTrace();
				}
			}
			if (preparedStatement != null) {
				try {
					preparedStatement.close();
				} catch (SQLException e) {
					e.printStackTrace();
				}
			}
			//ConnectionManager.closeConnection(connection);
		}

		return customer;
	}	
	
	//Read All
	public List<Customer> selectAll() {
		Statement statement = null;
		ResultSet resultSet = null;
		List<Customer> customers = new LinkedList<Customer>();
		try {
			statement = connection.createStatement();
			resultSet = statement.executeQuery("SELECT * FROM tblCustomers");

			Customer customer = null;
			while(resultSet.next()){
				customer = new Customer();
				customer.setId(Integer.parseInt(resultSet.getString("id")));
				customer.setName(resultSet.getString("name"));
				customer.setAge(Integer.parseInt(resultSet.getString("age")));
				customer.setResidentialArea(resultSet.getString("residentialArea"));
				customer.setPhone(resultSet.getString("phone"));
				customer.setRating(Integer.parseInt(resultSet.getString("rating")));
				customers.add(customer);
			}

			/*resultSet.close();
			statement.close();*/
		} catch (SQLException e) {
			e.printStackTrace();
		}
		finally {
			if (resultSet != null) {
				try {
					resultSet.close();
				} catch (SQLException e) {
					e.printStackTrace();
				}
			}
			if (statement != null) {
				try {
					statement.close();
				} catch (SQLException e) {
					e.printStackTrace();
				}
			}
			//ConnectionManager.closeConnection(connection);
		}
		System.out.println(customers);
		return customers;
	}


	//Update
	public void update() {
		Customer customer = null;
		try {
			Object deserializedObject = SerializationUtil.deserialize("c:\\editCustomer.out");
			customer = (Customer) deserializedObject;
		} catch (ClassNotFoundException e1) {
			e1.printStackTrace();
		} catch (IOException e1) {
			e1.printStackTrace();
		}
		
		PreparedStatement preparedStatement = null;

		try {
			preparedStatement = connection.prepareStatement("UPDATE tblCustomers SET " +
					"name = ?, age = ?, residentialArea = ?, phone = ?, rating = ? WHERE id = ?");

			preparedStatement.setString(1, customer.getName());
			preparedStatement.setInt(2, customer.getAge());
			preparedStatement.setString(3, customer.getResidentialArea());
			preparedStatement.setString(4, customer.getPhone());
			preparedStatement.setInt(5, customer.getRating());
			preparedStatement.setInt(6, customer.getId());
			preparedStatement.executeUpdate();

		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			if (preparedStatement != null) {
				try {
					preparedStatement.close();
				} catch (SQLException e) {
					e.printStackTrace();
				}
			}
			//ConnectionManager.closeConnection(connection);
		}
	}

	//Delete
	public void delete(int id) {
		PreparedStatement preparedStatement = null;

		try {
			preparedStatement = connection.prepareStatement("DELETE FROM tblCustomers WHERE id = ?");
			preparedStatement.setInt(1, id);
			preparedStatement.executeUpdate();

			System.out.println("DELETE FROM tblCustomers WHERE id = ?");

		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			if (preparedStatement != null) {
				try {
					preparedStatement.close();
				} catch (SQLException e) {
					e.printStackTrace();
				}
			}
			//ConnectionManager.closeConnection(connection);
		}
	}

	
	public int selectMaxId() {
		Statement statement = null;
		ResultSet resultSet = null;
		int id = 0;
		try {
			statement = connection.createStatement();
			resultSet = statement.executeQuery("SELECT max(id) as max_val FROM tblCustomers");

			while(resultSet.next()){
				id = Integer.parseInt(resultSet.getString("max_val"));
			}

			/*resultSet.close();
			statement.close();*/
		} catch (SQLException e) {
			e.printStackTrace();
		}
		finally {
			if (resultSet != null) {
				try {
					resultSet.close();
				} catch (SQLException e) {
					e.printStackTrace();
				}
			}
			if (statement != null) {
				try {
					statement.close();
				} catch (SQLException e) {
					e.printStackTrace();
				}
			}
			//ConnectionManager.closeConnection(connection);
		}
		
		return id;
	}
}
